<?php

namespace App\Http\Controllers;

use App\Models\SectNotice;
use Illuminate\Http\Request;

class SectNoticeController extends Controller
{
    function list() {
        $pn = request()->input('pn', config('api.page_num'));
        $sectId = request()->input('sect_id', 0);
        $list = SectNotice::where('sect_id', $sectId)
            ->select('id', 'user_id', 'context', 'created_at')
            ->with('user:id,name')
            ->orderBy('created_at', 'desc')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);
    }

    public function publish()
    {
        $sectId = request()->input('sect_id', 0);
        $userId = request()->input('user_id', 0);
        $context = request()->input('context', '');

        if ($context == '') {
            return response()->json(['recode' => 0,
                'msg' => '内容不能为空']);
        }

        $notice = new SectNotice;
        $notice->sect_id = $sectId;
        $notice->user_id = $userId;
        $notice->context = $context;

        $notice->save();

        return response()->json(['recode' => 1,
            'msg' => '发布成功']);

    }
}
